import { useState } from 'react';
import an58 from '@mmstudio/an000058';
import AddButton from '../../../components/addbutton';
import api from '../../../atoms/api';
import { Message as M1, Result as R1 } from '../../api/admin/role/add.api';
import Form from './form';

export default function Add() {
	const [d, setd] = useState({} as M1);
	return <>
		<AddButton dlgtitle='新增角色' onSave={async () => {
			if (!d
				|| !d.name
			) {
				return '角色名称不能为空';
			}
			const res = await an58<R1, M1>(api['/api/admin/role/add'], 'post', d);
			if (res.ok === true) {
				return true;
			}
			return res.message;
		}} >
			<Form data={d} onChange={(key, val) => {
				setd((pre) => {
					return {
						...pre,
						[key]: val
					};
				});
			}} />
		</AddButton>
	</>;
}
